const express = require('express');
const router = express.Router();
const puppeteer = require('puppeteer');
const fs = require('fs');
const { v4: uuidV4 } = require('uuid');
const { PDFDocument } = require('pdf-lib');
const config = require("../config");

router.get('/', async (req, res, next) => {
  const { reportId, dirName = '', reportName } = req.query || {};
  if (!reportId) {
    res.send({
      code: 40000,
      msg: "请输入reportId"
    });
    return;
  }

  // const folder = `/app/pdfReport/${dirName}`; // 部署使用
  const folder = `d:/report/${dirName}`; // 本地测试使用
  fs.access(folder, err => {
    if (err) {
      fs.mkdir(folder, { recursive: true }, err => {
        if (err) throw err;
      });
    } else {
      console.log('dir exists');
    }
  });
  try {
    // 启动无头浏览器
    const browser = await puppeteer.launch({
      args: ['--no-sandbox', '--disable-setuid-sandbox'],
      headless: true,
    });

    const defaultConfig = {
      format: 'A4',
      printBackground: true,
      margin: {
        top: '2cm',
        right: '2cm',
        bottom: '2cm',
        left: '2cm',
      },
    };

    // 封面
    const reportCover = await browser.newPage();
    // 不再有网络连接时触发
    await reportCover.goto(`${config.reportPageUrl}report-cover?reportName=${reportName}`, { waitUntil: 'networkidle0' });
    const reportCoverBuffer = await reportCover.pdf({
      ...defaultConfig,
      displayHeaderFooter: false,
    });
    // 关闭页面
    reportCover.close();

    // 内容
    const reportContent = await browser.newPage();
    // 不再有网络连接时触发
    await reportContent.goto(`${config.reportPageUrl}?reportId=${reportId}`, { waitUntil: 'networkidle0' });
    const pdfConfig = {
      ...defaultConfig,
      displayHeaderFooter: true,
      headerTemplate: `<div></div>`,
      footerTemplate: '<div style="width:100%;text-align:right;margin-right: 20px;font-size:10px"><span class="pageNumber"></span></div>',
    };
    const reportContentBuffer = await reportContent.pdf({ ...pdfConfig });
    // 关闭页面
    reportContent.close();
    // 关闭 chromium
    browser.close();

    // 合并pdf
    const pdfDoc = await PDFDocument.create();
    const coverDoc = await PDFDocument.load(reportCoverBuffer);
    const [coverPage] = await pdfDoc.copyPages(coverDoc, [0]);
    pdfDoc.addPage(coverPage);
    const reportDoc = await PDFDocument.load(reportContentBuffer);
    const reportPages = await pdfDoc.copyPages(reportDoc, reportDoc.getPageIndices());
    reportPages.forEach((page) => {
      pdfDoc.addPage(page);
    });
    const pdfBytes = await pdfDoc.save();
    fs.writeFileSync(`${folder}/${reportName || reportId || uuidV4()}.pdf`, pdfBytes);

    res.set({
      'Content-Type': 'application/pdf',
    });
    res.send(Buffer.from(pdfBytes));
  } catch (error) {
    res.send({
      code: 40001,
      msg: '页面渲染出错啦',
    });
  }

  // res.send({
  //   code: 200,
  // });
});

module.exports = router;
